#include <riscv/sbi.h>
#include <linux/errno.h>

static void __sbi_set_timer_v01(uint64_t stime_value)
{
    sbi_ecall(SBI_EXT_0_1_SET_TIMER, 0, stime_value, 0, 0, 0, 0, 0);
}

void sbi_set_timer(uint64_t stime_value)
{
    __sbi_set_timer_v01(stime_value);
}

void sbi_console_putchar(int ch)
{
    sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
}

int sbi_err_map_linux_errno(int err)
{
    int ret;

    switch (err)
    {
    case SBI_SUCCESS:
        ret = 0;
        break;
    case SBI_ERR_DENIED:
        ret = -EPERM;
        break;
    case SBI_ERR_INVALID_PARAM:
        ret = -EINVAL;
        break;
    case SBI_ERR_INVALID_ADDRESS:
        ret = -EFAULT;
        break;
    case SBI_ERR_NOT_SUPPORTED:
    case SBI_ERR_FAILURE:
    default:
        ret = -ENOTSUPP;
        break;
    };

    return ret;
}

void sbi_init(void)
{
}
